import time
from selenium.common.exceptions import StaleElementReferenceException
from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Common.do_mysql import DoMysql
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.role_locator import Rolelocator
from Case.python_ui.Test_data.role_data import Role
from Case.python_ui.Test_data.organization_data import Organization
from Case.python_ui.Common.do_mysql import DoMysql

class   RolePage(Basepage):


    def entry_the_role_page(self,cmdopt):
        doc='进入role主页面'
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(OrganizationLocator.user_management_tab,doc)
                self.move_mouse_to_click_element(OrganizationLocator.user_management_tab,doc)
                time.sleep(2)
                self.wait_elevisble(Rolelocator.role_tab,doc)
                self.move_mouse_to_click_element(Rolelocator.role_tab,doc)
                break
            except:
                self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
                time.sleep(5)
                continue
        self.refresh_role_name()
        self.wait_elevisble(Rolelocator.page_total,doc)

    # 重制查询条件
    def refresh_role_name(self):
        doc = '重置筛查询条件'
        time.sleep(2)
        self.wait_elevisble(Rolelocator.role_name_search_input, doc)
        self.double_click(Rolelocator.role_name_search_input, doc)
        self.keyboard_event(Rolelocator.role_name_search_input, doc)
        time.sleep(1)
        self.move_mouse_to_click_element(Rolelocator.role_name_search_button,doc)
        time.sleep(3)

    def role_list(self,cmdopt):
        doc='角色列表'
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        time.sleep(5)
        self.wait_elevisble(Rolelocator.role_name,doc)
        time.sleep(5)
        res=self.get_element(Rolelocator.role_name,doc).text
        return res

    def query_role(self,role_name):
        doc='查询角色'
        self.wait_elevisble(Rolelocator.role_name_search_input,doc)
        #self.keyboard_event(Rolelocator.role_name_search_input,doc)
        self.refresh_role_name()
        self.send_keys(Rolelocator.role_name_search_input,role_name,doc)
        time.sleep(5)
        self.click_element(Rolelocator.role_name_search_button,doc)
        time.sleep(2)
        self.wait_elevisble(Rolelocator.query_result_element,doc)
        time.sleep(2)
        res = self.get_element(Rolelocator.role_name,doc).text
        return  res

    def  create_role(self,cmdopt):
        doc='创建一个角色'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        time.sleep(5)
        self.wait_elevisble(Rolelocator.create_role_button,doc)
        #self.click_element(Rolelocator.role_name)
        self.click_element(Rolelocator.create_role_button,doc)
        self.wait_elevisble(Rolelocator.create_role_name,doc)
        role_name=CommonTool.generate_string()
        self.send_keys(Rolelocator.create_role_name,role_name,doc)
        time.sleep(2)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.wait_elevisble(Rolelocator.basic_information_button,doc)
        self.click_element(Rolelocator.creat_next_button,doc)
        time.sleep(3)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.wait_elevisble(Rolelocator.add_success_prompt,doc)
        time.sleep(2)
        self.wait_elevisble(Rolelocator.role_name_search_input,doc)
        self.send_keys(Rolelocator.role_name_search_input,role_name,doc)
        self.click_element(Rolelocator.role_name_search_button,doc)
        time.sleep(5)
        self.wait_elevisble(Rolelocator.role_name,doc)
        res=self.get_element(Rolelocator.role_name,doc).text
        return res ,role_name

    def delete_role(self,role_name,cmdopt):
        doc='删除角色'
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        self.query_role(role_name)
        self.move_mouse_to_click_element(Rolelocator.role_operation_button,doc)
        self.wait_elevisble(Rolelocator.role_delete,doc)
        time.sleep(2)
        self.click_element(Rolelocator.role_delete,doc)
        time.sleep(5)
        self.wait_elevisble(Rolelocator.role_I_acknowledge,doc)
        time.sleep(2)
        self.click_element(Rolelocator.role_I_acknowledge,doc)
        time.sleep(2)
        self.click_element(Rolelocator.role_ok_confirm,doc)
        self.wait_elevisble(Rolelocator.add_success_prompt,doc)
        time.sleep(5)
        res = self.get_elements(Rolelocator.role_name,doc)
        return res

    def edit_role(self,role_name,cmdopt):
        doc='编辑角色'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        self.query_role(role_name)
        edit_role_name=CommonTool.generate_edit_role_name()
        try:
            time.sleep(5)
            self.click_element(Rolelocator.role_edit,doc)
        except StaleElementReferenceException:
            self.wait_elevisble(Rolelocator.role_edit,doc)
            self.click_element(Rolelocator.role_edit,doc)
        self.keyboard_event(Rolelocator.edit_role_name,doc)
        time.sleep(2)
        self.send_keys(Rolelocator.edit_role_name,edit_role_name,doc)
        time.sleep(2)
        self.click_element(Rolelocator.role_ok_confirm,doc)
        self.wait_elevisble(Rolelocator.role_name_search_input,doc)
        self.keyboard_event(Rolelocator.role_name_search_input,doc)
        self.send_keys(Rolelocator.role_name_search_input, edit_role_name,doc)
        self.click_element(Rolelocator.role_name_search_button,doc)
        time.sleep(5)
        self.wait_elevisble(Rolelocator.role_name,doc)
        res=self.get_element(Rolelocator.role_name,doc).text
        return res ,edit_role_name

    def  edit_role_permissions(self):
         doc='编辑角色的权限'
         self.wait_elevisble(Rolelocator.role_operation_button,doc)
         self.click_element(Rolelocator.role_operation_button,doc)
         time.sleep(2)
         self.wait_elevisble(Rolelocator.role_permission_edit,doc)
         self.click_element(Rolelocator.role_permission_edit,doc)
         #self.wait_elevisble(Rolelocator.bill_permission,doc)
        # self.click_element(Rolelocator.bill_permission,doc)
         time.sleep(5)
         self.click_element(Rolelocator.bill_permission_edit_next,doc)
         self.wait_elevisble(Rolelocator.add_success_prompt,doc)



    def view_role_information(self,role_name,cmdopt):
        doc='查看角色的信息'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
        self.wait_elevisble(Rolelocator.role_name,doc)
        self.click_element(Rolelocator.role_name,doc)
        self.wait_elevisble(Rolelocator.overview_tab,doc)
        overview=self.get_element(Rolelocator.overview_tab,doc).text
        role_premission=self.get_element(Rolelocator.role_permission_tab,doc).text
        time.sleep(2)
        self.click_element(Rolelocator.back_to_role_page,doc)
        self.wait_elevisble(Rolelocator.role_name_search_input,doc)
        self.send_keys(Rolelocator.role_name_search_input,role_name,doc)
        self.click_element(Rolelocator.role_name_search_button,doc)
        return  overview ,role_premission


    def permission_point_control_for_bill_pending_summary(self,cmdopt):
        doc='bill_pending_summary菜单权限控制'
        time.sleep(2)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        self.wait_elevisble(Rolelocator.role_name,doc)
        self.click_element(Rolelocator.create_role_button,doc)
        self.wait_elevisble(Rolelocator.create_role_name,doc)
        role_name = CommonTool.generate_string()
        self.send_keys(Rolelocator.create_role_name, role_name,doc)
        time.sleep(2)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.wait_elevisble(Rolelocator.spending_summary,doc)
        self.click_element(Rolelocator.spending_summary,doc)
        time.sleep(2)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.wait_elevisble(Rolelocator.add_success_prompt,doc)
        return role_name


    def get_bill_pending_summary(self):
            doc='获取bill_pending_summary菜单'
            ele=self.get_elements(Rolelocator.spending_summary_tab,doc)
            return len(ele)



    def permission_point_control_for_order_management(self,cmdopt):
        doc='order_management菜单权限控制'
        time.sleep(2)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.entry_the_role_page(cmdopt)
        self.wait_elevisble(Rolelocator.role_name,doc)
        self.click_element(Rolelocator.create_role_button,doc)
        self.wait_elevisble(Rolelocator.create_role_name,doc)
        role_name = CommonTool.generate_string()
        self.send_keys(Rolelocator.create_role_name, role_name,doc)
        time.sleep(2)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.wait_elevisble(Rolelocator.order_management,doc)
        self.click_element(Rolelocator.order_management,doc)
        time.sleep(2)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.click_element(Rolelocator.creat_next_button,doc)
        self.wait_elevisble(Rolelocator.add_success_prompt,doc)
        return role_name

    def get_order_management(self):
            doc='获取order_management菜单'
            ele=self.get_elements(Rolelocator.order_management_tab,doc)
            return len(ele)